/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmVector.java
 *
 * Created on 02-12-2014, 10:44:46 AM
 */
package presentacion;

/**
 *
 * @author jhow
 */
import Negocio.Entero;
import Negocio.vectorEntero;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class frmVector extends javax.swing.JFrame {

    /** Creates new form frmVector */
    public frmVector() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnDimension = new javax.swing.JButton();
        txtDimension = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnOrdenar = new javax.swing.JButton();
        btnInvertirV = new javax.swing.JButton();
        btnBuscaE = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnEliminaP = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnDimension.setText("Dimensionar");
        btnDimension.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDimensionActionPerformed(evt);
            }
        });

        jLabel1.setText("Dimension");

        btnOrdenar.setText("Ordenar Vector");
        btnOrdenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrdenarActionPerformed(evt);
            }
        });

        btnInvertirV.setText("Invertir");
        btnInvertirV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInvertirVActionPerformed(evt);
            }
        });

        btnBuscaE.setText("Buscar");
        btnBuscaE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscaEActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        btnEliminaP.setText("Eliminar Primo");
        btnEliminaP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminaPActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnOrdenar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnInvertirV)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnBuscaE)
                                .addGap(18, 18, 18)
                                .addComponent(btnEliminaP))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDimension, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnDimension))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(btnSalir)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDimension)
                    .addComponent(txtDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOrdenar)
                    .addComponent(btnInvertirV)
                    .addComponent(btnBuscaE)
                    .addComponent(btnEliminaP, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnSalir)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private void Dimensionar(int CantidadColumna){
    DefaultTableModel modelo= new DefaultTableModel();
    this.jTable1.setModel(modelo);
    for(int i=0;i<=CantidadColumna-1;i++){
        modelo.addColumn(Integer.toString(i));
       }
    Object[] fila=new Object[1];
    modelo.addRow(fila);
    
}
    private void btnDimensionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDimensionActionPerformed
this.Dimensionar(Integer.parseInt(this.txtDimension.getText() ));   
// TODO add your handling code here:
    }//GEN-LAST:event_btnDimensionActionPerformed

    private void btnOrdenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrdenarActionPerformed
vectorEntero objVector=new vectorEntero();
this.CargarVector(objVector);
objVector.Ordenar();
this.MostrarVector(objVector);
        // TODO add your handling code here:
    }//GEN-LAST:event_btnOrdenarActionPerformed

    private void btnInvertirVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInvertirVActionPerformed
        vectorEntero objVector=new vectorEntero();
this.CargarVector(objVector);
objVector.InvertirV();
this.MostrarVector(objVector);
        // TODO add your handling code here:
    }//GEN-LAST:event_btnInvertirVActionPerformed

    private void btnBuscaEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscaEActionPerformed
        vectorEntero objVector=new vectorEntero();
        this.CargarVector(objVector);
        String  ele=JOptionPane.showInputDialog(null,"elemento");
        
        if(objVector.Existe(Integer.parseInt(ele))){
             JOptionPane.showMessageDialog(null,"Se encontro :   "+ele+" en el vector" );
            }
        else{
                JOptionPane.showMessageDialog(null," NOoo Se Encontro :   "+ele+" en el vector");
            }
//this.MostrarVector(objVector);
        // TODO add your handling code here:
    }//GEN-LAST:event_btnBuscaEActionPerformed

    private void btnEliminaPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminaPActionPerformed
        vectorEntero objVector=new vectorEntero();
        this.CargarVector(objVector);
        objVector.eliP();
        this.MostrarVector(objVector);
// TODO add your handling code here:
    }//GEN-LAST:event_btnEliminaPActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        frmPrincipal f1=new frmPrincipal();
        f1.setVisible(true);
        this.setVisible(false);  // TODO add your handling code here:
}//GEN-LAST:event_btnSalirActionPerformed
private void CargarVector(Negocio.vectorEntero objvector){
    for(int i=0;i<=Integer.parseInt(this.txtDimension.getText())-1;i++ ){
        Entero ObjEntero=new Entero();
        ObjEntero.setNum(Integer.parseInt(jTable1.getValueAt(0, i).toString()));
        objvector.Insertar(ObjEntero);
    }
    
}
private void MostrarVector(vectorEntero objvector){
    this.Dimensionar(objvector.getN());
    for(int i=0;i<=objvector.getN()-1;i++){
    jTable1.setValueAt( objvector.LeerVector(i).getNum(),0,i);   
    }
    
}

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmVector().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscaE;
    private javax.swing.JButton btnDimension;
    private javax.swing.JButton btnEliminaP;
    private javax.swing.JButton btnInvertirV;
    private javax.swing.JButton btnOrdenar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    public javax.swing.JTable jTable1;
    private javax.swing.JTextField txtDimension;
    // End of variables declaration//GEN-END:variables
}
